package com.zzzzzz.oms.timeLimit;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.util.WebUtils;

import com.zzzzzz.core.datagrid.BaseData;
import com.zzzzzz.core.datagrid.BaseQueryForm;
import com.zzzzzz.utils.page.Page;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zzzzzz.oms.GCS;
import com.zzzzzz.oms.driver.Driver;
import com.zzzzzz.plugins.poi.ExportExcel;
import com.zzzzzz.plugins.shiro.ShiroUtils;

/**
 * @author hing
 * @version 1.0.0
 */
@Controller
public class TimeLimitWeb {

	@Resource
	public TimeLimitService timeLimitService;
	@Resource
	public TimeLimitDao timeLimitDao;

	// 打开时效界面
	@RequestMapping(value = "/timeLimit/list", method = RequestMethod.GET)
	public String list() {
		return "oms/timeLimit";
	}

	// 增加、更新
	@RequestMapping(value = "/timeLimit/save", method = RequestMethod.POST)
	@ResponseBody
	public BaseData save(@RequestBody @Valid TimeLimit timeLimit, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			return new BaseData(GCS.ERRCD_PARAM);
		}

		timeLimitService.save(timeLimit, ShiroUtils.findUser());
		return new BaseData("0");
	}
	
	// 停用
	@RequestMapping(value = "/timeLimit/updStByIds", method = RequestMethod.POST)
	@ResponseBody
	public BaseData updStByIds(@RequestParam(value = "ids", required = true) List<Long> ids, @RequestParam(value = "st", required = true) Integer st) throws Exception {
		timeLimitDao.updStByIds(ids, st, ShiroUtils.findUser());
		return new BaseData("0");
	}	  
	
	// 编辑
	@RequestMapping(value = "/timeLimit/id/{id}", method = RequestMethod.GET)
	@ResponseBody 
	public BaseData findById(@PathVariable Long id) {
		TimeLimit timeLimit = timeLimitDao.findById(id);
		return new BaseData(timeLimit, null);
	}
	
	// 动态查询
	@RequestMapping(value = "/timeLimit/findListBy", method = RequestMethod.POST)
	@ResponseBody
	public BaseData findListBy(@RequestBody BaseQueryForm baseQueryForm) {
		Page page;
		List<TimeLimit> list;
		BaseData baseData;
		if(baseQueryForm.getPageNb()==null&&baseQueryForm.getPageSize()==null){
			list = timeLimitDao.findListBy(null,baseQueryForm.getFfMap(),ShiroUtils.findUser());
			baseData=new BaseData(list, null);
		}else{
			page=baseQueryForm.getPage();
			list = timeLimitDao.findListBy(page,baseQueryForm.getFfMap(),ShiroUtils.findUser());
			baseData=new BaseData(list, page.getTotal());
		}
		return baseData;
	}
    
	// 导出
	 @RequestMapping(value = "/timeLimit/export")
	    public String exportFileTemplate(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
			try {
				
				Map<String, Object> params = WebUtils.getParametersStartingWith(request, null);
				
				//Page page=baseQueryForm.getPage();
	            String fileName = "回单时效数据.xlsx";
	            
	            List<TimeLimit> list =timeLimitDao.findListBy(null, params,ShiroUtils.findUser());
	  
	    		new ExportExcel("回单时效数据", TimeLimit.class, 1).setDataList(list).write(response, fileName).dispose();
	    		return null;
			} catch (Exception e) {
				redirectAttributes.addFlashAttribute("msg", "回单时效数据导出失败。");
				logger.warn("importFileTemplate", e);
			}
			return "redirect:/msg";
	    }
    private static final Logger logger = LoggerFactory.getLogger(TimeLimitWeb.class);
}
